package spe.model.spem;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.resource.ImageDescriptor;

/**
 * Author Dao Anh Vu
 * */
public abstract class SPEMElementImpl implements SPEMElement
{
	protected PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
	
	protected String name;
	protected String namePrefix;
	protected String id;
	protected String elementId;
	protected SPEMElement parent;
	protected ImageDescriptor imageDes;
	protected SPEMFactory factory;
	
	
	public void initialize()
	{
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener)
	{
		//System.out.println(String.format("[SPEMElementImpl] Element: %s; Listener: %s", this, listener));
		propertyChangeSupport.addPropertyChangeListener(listener);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener listener)
	{
		propertyChangeSupport.removePropertyChangeListener(listener);
	}
	
	@Override
	public void firePropertyChange(String propertyName, Object oldValue, Object newValue)
	{
		//System.out.println(String.format("[SPEMElement] Fire property change %s on %s", this, propertyName));
		propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
	}
	
	@Override
	public void setParent(SPEMElement p) 
	{
		SPEMElement oldValue = this.parent;
		this.parent = p;
		firePropertyChange("parent", oldValue, p);
	}
	
	@Override
	public SPEMElement getParent()
	{
		return parent;
	}
	
	public void setElementId(String id)
	{
		elementId = id;
	}
	
	public String getElementId()
	{
		return elementId;
	}
	
	@Override
	public String getId() 
	{
		// TODO Auto-generated method stub
		return id;
	}
	
	@Override
	public void setId(String id)
	{
		this.id = id;
	}

	@Override
	public String getNamePrefix() 
	{
		return namePrefix;
	}
	
	public void setNamePrefix(String namePrefix)
	{
		this.namePrefix = namePrefix;  
	}
	
	@Override
	public ImageDescriptor getIconDescriptor() 
	{
		return imageDes;
	}
	
	@Override
	public void setIconDescriptor(ImageDescriptor image)
	{
		imageDes = image;
	}

	@Override
	public SPEMFactory getFactory() 
	{
		// TODO Auto-generated method stub
		return factory;
	}

	@Override
	public void setFactory(SPEMFactory factory) 
	{
		this.factory = factory;
	}

	@Override
	public void setName(String newName) 
	{
		String oldValue = name;
		this.name = newName;
		firePropertyChange("name", oldValue, newName);
	}

	@Override
	public String getName() 
	{
		return name;
	}

	@Override
	public boolean isNameMandatory() 
	{
		// TODO Auto-generated method stub
		return true;
	}
	
	public Map<String, Object> getAttributes()
	{
		Map<String, Object> attrs = new HashMap<String, Object>();
		attrs.put("id", id);
		attrs.put("name", name);
		return attrs;
	}
	
	@Override
	public void register()
	{
		
	}
	
	@Override
	public void unregister()
	{
		
	}

}
